import { uniqueId } from "lodash";
// 设置文件
import setting from "@/setting.js";

/**
 * 给菜单数据补充上 path 字段
 * https://github.com/d2-projects/d2-admin/issues/209
 * @param {Array} menu 原始的菜单数据
 */
function supplementMenuPath(menu) {
  return menu.map((e) => ({
    ...e,
    path: e.path || uniqueId("d2-menu-empty-"),
    ...(e.children
      ? {
          children: supplementMenuPath(e.children),
        }
      : {}),
  }));
}

export default {
  namespaced: true,
  state: {
    // 顶栏菜单
    header: [],
    // 侧栏菜单
    aside: [],
    // 接口返回的aside
    savedAside: [],
    // 侧边栏收缩
    asideCollapse: setting.menu.asideCollapse,
  },
  actions: {
    /**
     * 设置侧边栏展开或者收缩
     * @param {Object} context
     * @param {Boolean} collapse is collapse
     */
    async asideCollapseSet({ state, dispatch }, collapse) {
      // store 赋值
      state.asideCollapse = collapse;
      // 持久化
      await dispatch(
        "d2admin/db/set",
        {
          // dbName: "sys",
          path: "menu.asideCollapse",
          value: state.asideCollapse,
          // user: true,
        },
        { root: true }
      );
    },
    /**
     * 设置菜单
     */
    async menuSet({ state, dispatch }, menu) {
      // store 赋值
      state.header = [...menu.map(({ children, ...restParams }) => restParams)];

      state.savedAside = menu;

      // 持久化
      await dispatch(
        "d2admin/db/set",
        {
          // dbName: "sys",
          path: "menu.header",
          value: state.header,
          // user: true,
        },
        { root: true }
      );
      await dispatch(
        "d2admin/db/set",
        {
          // dbName: "sys",
          path: "menu.savedAside",
          value: state.savedAside,
          // user: true,
        },
        { root: true }
      );
    },

    /**
     * 切换侧边栏展开和收缩
     * @param {Object} context
     */
    async asideCollapseToggle({ state, dispatch }) {
      // store 赋值
      state.asideCollapse = !state.asideCollapse;
      // 持久化
      await dispatch(
        "d2admin/db/set",
        {
          dbName: "sys",
          path: "menu.asideCollapse",
          value: state.asideCollapse,
          user: true,
        },
        { root: true }
      );
    },
    /**
     * 从持久化数据读取侧边栏展开或者收缩
     * @param {Object} context
     */
    async asideCollapseLoad({ state, dispatch }) {
      // store 赋值
      state.asideCollapse = await dispatch(
        "d2admin/db/get",
        {
          dbName: "sys",
          path: "menu.asideCollapse",
          defaultValue: setting.menu.asideCollapse,
          user: true,
        },
        { root: true }
      );
    },
    /**
     * 初始化菜单
     */
    async load({ state, dispatch, commit }) {
      // store 赋值
      state.header = await dispatch(
        "d2admin/db/get",
        {
          // dbName: "sys",
          path: "menu.header",
          // user: true,
        },
        { root: true }
      );

      const menu = await dispatch(
        "d2admin/db/get",
        {
          // dbName: "sys",
          path: "menu.savedAside",
          // user: true,
        },
        { root: true }
      );
      state.savedAside = menu;

      // 设置搜索栏数据
      commit("d2admin/search/init", menu, { root: true });
    },
  },
  mutations: {
    /**
     * @description 设置侧边栏菜单
     * @param {Object} state state
     * @param {Array} menu menu setting
     */
    asideSet(state, menu) {
      // store 赋值
      state.aside = supplementMenuPath(menu);
    },
    /**
     * 保存侧边栏的数据
     */
    savedAsideSet(state, menu) {
      // store 赋值
      state.savedAside = supplementMenuPath(menu);
    },
  },
};
